const { mapToArray, setCloudWords, setSingleWords } = require('../public/javascripts/tools')

function main(origin) {
    return extract(origin)
}

function extract(data) {
    data = arrayFlat(data)
    console.log(data.length,'^^^^^^^^^^^^^^')
    return getKeywords(data)
}

function arrayFlat(array, key) {
    const arr = array.map((item,index)=> {
        return item.engine_search_result
    })
    const res = [].concat(...arr)
    return res
}

/*
    词云数据处理，聚合关键字,高频词为对象数组
*/
function getKeywords(data) {
    const res = new Map()
    if(!Array.isArray(data) || data.length === 0) return res

    for(let i = 0; i < data.length; i++) {
        getJobWelfs(data[i], res)
        getOtherKeywords(data[i], res)
    }
    return mapToArray(res)
}

function getJobWelfs(data, map) {
    const res = map
    setCloudWords(data.jobwelf_list, map)
    return res
}

function getOtherKeywords(data, map) {
    const res = map
    setSingleWords(data.companytype_text, res)
    setSingleWords(data.attribute_text, res)
    setSingleWords(data.companyind_text, res)
    setSingleWords(data.job_title, res)
    setSingleWords(data.providesalary_text, res)
    setSingleWords(data.workarea_text, res)
    setSingleWords(data.companysize_text, res)
    return res
}
module.exports = main